package com.ly.auth.service;

import com.alibaba.fastjson.JSONObject;
import com.common.core.constant.Constants;
import com.common.core.constant.UserConstants;
import com.common.core.enums.UserStatus;
import com.common.core.exception.BaseException;
import com.common.core.utils.SecurityUtils;
import com.common.core.utils.StringUtils;
import com.domain.LoginUser;
import com.opfegin.api.RemoteLogService;
import com.opfegin.api.RemoteUserService;
import com.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 登录校验方法
 *
 * @author ruoyi
 */
@Component
public class SysLoginService {
    @Autowired
    private RemoteLogService remoteLogService;

    @Autowired
    private RemoteUserService remoteUserService;

    /**
     * 登录
     */
    public LoginUser login(String username, String password) {
        // 用户名或密码为空 错误
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password)) {
            throw new BaseException("用户/密码必须填写");
        }
        // 密码如果不在指定范围内 错误
        if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
                || password.length() > UserConstants.PASSWORD_MAX_LENGTH) {
            throw new BaseException("用户密码不在指定范围");
        }
        // 用户名不在指定范围内 错误
        if (username.length() < UserConstants.USERNAME_MIN_LENGTH
                || username.length() > UserConstants.USERNAME_MAX_LENGTH) {
            throw new BaseException("用户名不在指定范围");
        }
        // 查询用户信息
        //超级管理员的
        String str = "{\"expireTime\":1658410266782,\"ipaddr\":\"192.168.1.110\",\"loginTime\":1658367066782,\"permissions\":Set[\"*:*:*\"],\"roles\":Set[\"admin\"],\"sysUser\":{\"admin\":true,\"avatar\":\"http://127.0.0.1:9300/statics/2022/05/23/3dafdbdc-7f42-4b89-b19f-86ea83d057ff.jpeg\",\"checkPermission\":false,\"createBy\":\"admin\",\"createTime\":1521199980000,\"delFlag\":\"0\",\"dept\":{\"checkPermission\":false,\"children\":[],\"deptId\":100,\"deptName\":\"人事部\",\"finish\":false,\"haveActivity\":false,\"leader\":\"部门经理\",\"orderNum\":\"0\",\"params\":{\"@type\":\"java.util.LinkedHashMap\"},\"parentId\":0,\"status\":\"0\",\"userOpinion\":false},\"deptId\":100,\"email\":\"ry@163.com\",\"entId\":1,\"finish\":false,\"haveActivity\":false,\"loginDate\":1521171180000,\"loginIp\":\"127.0.0.1\",\"nickName\":\"若依\",\"params\":{\"@type\":\"java.util.LinkedHashMap\"},\"password\":\"$2a$10$7JB720yubVSZvUI0rEqK/.VqGOZTH.ulu33dHOiBE8ByOhJIrdAu2\",\"phonenumber\":\"15888888888\",\"remark\":\"管理员\",\"roles\":[{\"admin\":true,\"checkPermission\":false,\"dataScope\":\"1\",\"deptCheckStrictly\":false,\"finish\":false,\"flag\":false,\"haveActivity\":false,\"menuCheckStrictly\":false,\"params\":{\"@type\":\"java.util.LinkedHashMap\"},\"roleId\":1,\"roleKey\":\"admin\",\"roleName\":\"超级管理员\",\"roleSort\":\"1\",\"status\":\"0\",\"userOpinion\":false}],\"sex\":\"1\",\"status\":\"0\",\"userId\":1,\"userName\":\"admin\",\"userOpinion\":false},\"token\":\"9b4a5a93-de54-4e65-a266-cfe01f8baa45\",\"userid\":1,\"username\":\"admin\"}";
        //普通用户的
//        String str = "{\"expireTime\":1658497578500,\"ipaddr\":\"127.0.0.1\",\"loginTime\":1658454378500,\"permissions\":Set[],\"roles\":Set[\"oilrecoveryteam\"],\"sysUser\":{\"admin\":false,\"avatar\":\"\",\"checkPermission\":false,\"createBy\":\"admin\",\"createTime\":1653931689000,\"delFlag\":\"0\",\"dept\":{\"checkPermission\":false,\"children\":[],\"deptId\":1,\"deptName\":\"企划部2\",\"finish\":false,\"haveActivity\":false,\"leader\":\"张三\",\"orderNum\":\"1\",\"params\":{\"@type\":\"java.util.LinkedHashMap\"},\"parentId\":0,\"status\":\"0\",\"userOpinion\":false},\"deptId\":1,\"email\":\"1558842988@qq.com\",\"entId\":1,\"finish\":false,\"haveActivity\":false,\"loginIp\":\"\",\"nickName\":\"李1\",\"params\":{\"@type\":\"java.util.LinkedHashMap\"},\"password\":\"$2a$10$jwf7doKauALR6ITIcAuSqeblV2rb5KngyErSr2p3A2szl1SvxaIOe\",\"phonenumber\":\"13199761123\",\"roles\":[{\"admin\":false,\"checkPermission\":false,\"dataScope\":\"1\",\"deptCheckStrictly\":false,\"finish\":false,\"flag\":false,\"haveActivity\":false,\"menuCheckStrictly\":false,\"params\":{\"@type\":\"java.util.LinkedHashMap\"},\"roleId\":2,\"roleKey\":\"oilrecoveryteam\",\"roleName\":\"采油队管理人员\",\"roleSort\":\"2\",\"status\":\"0\",\"userOpinion\":false}],\"sex\":\"0\",\"status\":\"0\",\"userId\":3,\"userName\":\"ly1\",\"userOpinion\":false},\"token\":\"10ba7069-bba1-48a2-bade-a04ec3cce44b\",\"userid\":3,\"username\":\"ly1\"}";
        LoginUser userResult = JSONObject.parseObject(str, LoginUser.class);


        SysUser user = userResult.getSysUser();
        if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {

            throw new BaseException("对不起，您的账号：" + username + " 已被删除");
        }
        if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
            throw new BaseException("对不起，您的账号：" + username + " 已停用");
        }
        if (!SecurityUtils.matchesPassword(password, user.getPassword())) {
            throw new BaseException("用户不存在/密码错误");
        }
        return userResult;
    }


    public void logout(String loginName) {
        remoteLogService.saveLogininfor(loginName, Constants.LOGOUT, "退出成功");
    }
}
